package cn.ekuma.epos.subject;

import java.util.Date;
import java.util.Properties;

import javax.swing.JPanel;

import cn.ekuma.data.ui.swing.AbstractParentDAOJEditor;
import cn.ekuma.data.ui.swing.JChildDAOEditorPanel;
import cn.ekuma.epos.datalogic.I_DataLogicERP;
import cn.ekuma.epos.subject.model.SubjectCustomerTableModel;
import cn.ekuma.epos.subject.model.SubjectProductTableModel;

import com.openbravo.bean.erp.subject.Subject;
import com.openbravo.beans.JCalendarDialog;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.gui.ComboBoxValEntry;
import com.openbravo.data.gui.ComboBoxValModel;
import com.openbravo.data.user.DirtyManager;
import com.openbravo.format.Formats;
import com.openbravo.pos.base.AppLocal;

import java.awt.BorderLayout;
import javax.swing.JTabbedPane;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.Dimension;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.JTextArea;

public class SubjectEditor extends AbstractParentDAOJEditor<Subject,String> {
	private JTextField name;
	private JTextField startTime;
	private JTextField endTime;
	private JTextField evaluation;
	private JTextArea memo;
	private ComboBoxValModel m_stateModel;
	private JComboBox subjectState;
	
	JChildDAOEditorPanel subjectProductPanel;
	JChildDAOEditorPanel subjectCustomerPanel;
	private JTabbedPane jTabbedPane1;

	public SubjectEditor(I_DataLogicERP dlSales,DirtyManager dirty) {
		super(dirty);
		
		initComponents();
		m_stateModel=new ComboBoxValModel();
		m_stateModel.add(new ComboBoxValEntry(Subject.STATE_Init,AppLocal.getIntString("list.Subject.STATE_Init")));
		m_stateModel.add(new ComboBoxValEntry(Subject.STATE_inProcess,AppLocal.getIntString("list.STATE_inProcess")));
		m_stateModel.add(new ComboBoxValEntry(Subject.STATE_Complete,AppLocal.getIntString("list.STATE_Complete")));
		m_stateModel.add(new ComboBoxValEntry(Subject.STATE_Cancel,AppLocal.getIntString("list.Subject.STATE_Cancel")));
		subjectState.setModel(m_stateModel);
		
		name.getDocument().addDocumentListener(dirty);
		startTime.getDocument().addDocumentListener(dirty);
		endTime.getDocument().addDocumentListener(dirty);
		evaluation.getDocument().addDocumentListener(dirty);
		memo.getDocument().addDocumentListener(dirty);
		
		
		subjectProductPanel=new  JChildDAOEditorPanel(dlSales,new SubjectProductTableModel(),new SubjectProductEditor(dlSales,new DirtyManager()));
        jTabbedPane1.add(subjectProductPanel.getTabName(), subjectProductPanel);
        addObjectChangeListener(subjectProductPanel);
        
        subjectCustomerPanel=new  JChildDAOEditorPanel(dlSales,new SubjectCustomerTableModel(),new SubjectCustomerEditor(dlSales,null, new DirtyManager()));
        jTabbedPane1.add(subjectCustomerPanel.getTabName(), subjectCustomerPanel);
        addObjectChangeListener(subjectCustomerPanel);
	}
	private void initComponents() {
		setLayout(new BorderLayout(0, 0));
		
		JPanel panel = new JPanel();
		panel.setPreferredSize(new Dimension(10, 30));
		add(panel, BorderLayout.NORTH);
		panel.setLayout(null);
		
		JLabel labname = new JLabel();
		labname.setBounds(10, 8, 83, 15);
		labname.setText(AppLocal.getIntString("label.Subject.name"));
		panel.add(labname);
		
		name = new JTextField();
		name.setBounds(103, 5, 166, 21);
		panel.add(name);
		
		JLabel labstate = new JLabel(AppLocal.getIntString("label.Subject.State"));
		labstate.setBounds(288, 8, 74, 15);
		panel.add(labstate);
		
		subjectState = new JComboBox();
		subjectState.setBounds(364, 5, 108, 21);
		panel.add(subjectState);
		
		jTabbedPane1 = new JTabbedPane(JTabbedPane.TOP);
		add(jTabbedPane1, BorderLayout.CENTER);
		
		JPanel panel_1 = new JPanel();
		jTabbedPane1.addTab("New tab", null, panel_1, null);
		panel_1.setLayout(null);
		
		JLabel label_1 = new JLabel();
		label_1.setText(AppLocal.getIntString("Label.StartDate"));
		label_1.setBounds(10, 15, 89, 15);
		panel_1.add(label_1);
		
		startTime = new JTextField();
		startTime.setText((String) null);
		startTime.setPreferredSize(new Dimension(200, 25));
		startTime.setBounds(109, 10, 200, 25);
		panel_1.add(startTime);
		
		JButton btnDateStart = new JButton();
		btnDateStart.setBounds(319, 10, 54, 25);
		 btnDateStart.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/date.png"))); // NOI18N
	        btnDateStart.setPreferredSize(new java.awt.Dimension(50, 25));
	        btnDateStart.addActionListener(new java.awt.event.ActionListener() {
	            public void actionPerformed(java.awt.event.ActionEvent evt) {
	                btnDateStartActionPerformed(evt);
	            }
	        });
	      panel_1.add(btnDateStart);
		
		JLabel label_2 = new JLabel();
		label_2.setText(AppLocal.getIntString("Label.EndDate"));
		label_2.setBounds(10, 46, 89, 15);
		panel_1.add(label_2);
		
		endTime = new JTextField();
		endTime.setText((String) null);
		endTime.setPreferredSize(new Dimension(200, 25));
		endTime.setBounds(109, 41, 200, 25);
		panel_1.add(endTime);
		
		JButton btnDateEnd = new JButton();
		btnDateEnd.setBounds(319, 41, 54, 25);
		 btnDateEnd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/date.png"))); // NOI18N
	        btnDateEnd.setPreferredSize(new java.awt.Dimension(50, 25));
	        btnDateEnd.addActionListener(new java.awt.event.ActionListener() {
	            public void actionPerformed(java.awt.event.ActionEvent evt) {
	                btnDateEndActionPerformed(evt);
	            }
	        });
	    panel_1.add(btnDateEnd);
	    
	    memo = new JTextArea();
		memo.setBounds(10, 104, 479, 205);
		panel_1.add(memo);
		
		JLabel lblNewLabel_1 = new JLabel(AppLocal.getIntString("label.Subject.Evaluation"));
		lblNewLabel_1.setBounds(10, 76, 89, 15);
		panel_1.add(lblNewLabel_1);
		
		evaluation = new JTextField();
		evaluation.setBounds(109, 73, 238, 21);
		panel_1.add(evaluation);
		evaluation.setColumns(10);
	}

	@Override
	public void refresh() {
	}

	@Override
	public Class getEditClass() {
		return Subject.class;
	}
	@Override
	public Subject createNew(Properties prop) {
		Subject obj=new Subject();
		return obj;	
}
	@Override
	public void rebuild(Subject obj) throws BasicException {
		obj.setName(name.getText());
		obj.setStartDate((Date) Formats.TIMESTAMP.parseValue(startTime.getText()));
		obj.setEndDate((Date) Formats.TIMESTAMP.parseValue(endTime.getText()));
		obj.setEvaluation(evaluation.getText());
		obj.setMemo(memo.getText());
		obj.setState((String) m_stateModel.getSelectedKey());
	}
	
	@Override
	public void updateUI(boolean editAble, int state) {
		name.setText(obj.getName());
		startTime.setText(Formats.TIMESTAMP.formatValue(obj.getStartDate()));
		endTime.setText(Formats.TIMESTAMP.formatValue(obj.getEndDate()));
		evaluation.setText(obj.getEvaluation());
		memo.setText(obj.getMemo());
		m_stateModel.setSelectedKey(obj.getState());
		
		name.setEditable(editAble);
		startTime.setEditable(editAble);
		endTime.setEditable(editAble);
		evaluation.setEditable(editAble);
		memo.setEditable(editAble);
		subjectState.setEditable(editAble);
		
	}

	private void btnDateStartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDateStartActionPerformed
        Date date;
        try {
            date = (Date) Formats.TIMESTAMP.parseValue(startTime.getText());
        } catch (BasicException e) {
            date = null;
        }
        date = JCalendarDialog.showCalendarTimeHours(this, date);
        if (date != null) {
        	startTime.setText(Formats.TIMESTAMP.formatValue(date));
        }
}//GEN-LAST:event_btnDateStartActionPerformed

    private void btnDateEndActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDateEndActionPerformed
        Date date;
        try {
            date = (Date) Formats.TIMESTAMP.parseValue(endTime.getText());
        } catch (BasicException e) {
            date = null;
        }
        date = JCalendarDialog.showCalendarTimeHours(this, date);
        if (date != null) {
        	endTime.setText(Formats.TIMESTAMP.formatValue(date));
        }
}//GEN-LAST:event_btnDateEndActionPerformed
}
